﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.Data;
using System.Data;

namespace blackhouse.BlackApart.Data.Sql {
    public class ApartmentDataAccess : AdoApartmentDataAccess {

        #region Fields

        private string connectionString;
        private MsSqlAdoDataHelper dataHelper;

        #endregion

        #region Constructors

        public ApartmentDataAccess(string sqlConnectionString) {
            this.connectionString = sqlConnectionString;
            this.dataHelper = new MsSqlAdoDataHelper(this.connectionString);
            SqlVersioning versioning = new SqlVersioning(this.connectionString, this.GetType().Assembly);
        }

        #endregion

        #region AdoApartmentDataAccess Members

        public override int SaveApartment(int id, string name, int groupId, int locationId, int numberOfPeople, float areaSize) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ApartmentSave";
            this.dataHelper.AddParameter("Id", id, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("Name", name);
            this.dataHelper.AddParameter("NumberOfPeople", numberOfPeople, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("LocationId", locationId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("AreaSize", areaSize, System.Data.DbType.Single);
            this.dataHelper.AddParameter("GroupId", groupId);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        protected override IDataReader GetReaderForApartment(int apartmentId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ApartmentGet";
            this.dataHelper.AddParameter("Id", apartmentId, System.Data.DbType.Int32);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForApartmentsFree(DateTime startDate, DateTime endDate, int apartmentGroupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ApartmentGetAllFree";
            this.dataHelper.AddParameter("StartDate", startDate, System.Data.DbType.DateTime);
            this.dataHelper.AddParameter("EndDate", endDate, System.Data.DbType.DateTime);
            if (apartmentGroupId > 0)
                this.dataHelper.AddParameter("GroupId", apartmentGroupId, System.Data.DbType.Int32);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForApartmentsByGroup(int groupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ApartmentGetByGroup";
            this.dataHelper.AddParameter("GroupId", groupId, System.Data.DbType.Int32);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForApartmentPhotos(int apartmentId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PhotosApartmentGet";
            this.dataHelper.AddParameter("ApartmentId", apartmentId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForApartmentTypePhotos(int apartmentTypeId, string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PhotosApartmentTypeGet";
            this.dataHelper.AddParameter("ApartmentTypeId", apartmentTypeId, DbType.Int32);
            this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
            return this.dataHelper.Execute();
        }

        #endregion

    }
}
